package com.cvit.controller;

import java.beans.IntrospectionException;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cvit.model.Admission;
import com.cvit.model.Msg;
import com.cvit.model.Student;
import com.cvit.service.AdmissionService;
import com.cvit.service.StudentService;
import com.cvit.utils.Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class AdmissionController {
		
	@Autowired
	AdmissionService admissionService;
	@Autowired
	StudentService studentService;
	
	/**
	 * 查询录取表
	 * */
	@RequestMapping("/getAdmission")
	@ResponseBody
	public Msg getAdmissions(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
		PageHelper.startPage(pn, 5);
		List<Admission> admissions = admissionService.getAdmission();
		PageInfo<Admission> page = new PageInfo<Admission>(admissions, 5);
		return Msg.success().add("pageInfo", page);
	}
	
	/**
	 * 根据ID、考生号、身份证号、名字查询
	 * */
	@RequestMapping("/getAdmissionBy")
	@ResponseBody
	public Msg getAdmissionBy(Admission admission){
		Admission admissions = admissionService.getAdmissionBy(admission);
		return Msg.success().add("Admissions", admissions);
}
	
	/**
	 * 根据 身份证 查询录取信息
	 * */
	@RequestMapping("/getAdmissionByCardId")
	@ResponseBody
	public Msg getAdmissionByCardId(String cardId){
		Date day=new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHH");
		long ts1= Long.parseLong(df.format(day));
		long ts2 = 2018032307; 
		if(ts1>ts2){
			Student student = studentService.getStudnetByCardId(cardId);
			if(Utils.isNull(student)==true){
				return Msg.fail().add("code", "身份证号填写错误");//查询为空
			}else{
				Admission admissions = admissionService.getAdmissionByCardId(cardId);
				if(Utils.isNull(admissions)==true){
					return Msg.fail().add("code", "未录取");//查询为空
				}
					return Msg.success().add("Admissions", admissions);
			}
		}else
			return Msg.success().add("code", "未到公布时间");//未到录取时间
	}
	

	/**
	 * 导入Excel
	 * */
	@RequestMapping("/AdmissionImport")
	@ResponseBody
	public Msg impotr(HttpServletRequest request, Admission admission) throws Exception {
	     //获取上传的文件 
	     MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
	     MultipartFile file = multipart.getFile("upfile");
	     InputStream in = file.getInputStream();
	     //数据导入  
	     admissionService.importExcelInfo(in,file);
	     in.close();
	     return Msg.success();
	}
	
	/**
	 * 导出Excel
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping("/AdmissionExport")  
	public @ResponseBody void export(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException, UnsupportedEncodingException {  
		String admissionData = "录取信息表";
	    if(admissionData!=""){
			response.reset(); //清除buffer缓存  
	        Map<String,Object> map=new HashMap<String,Object>();  
	        // 指定下载的文件名  
	        response.setContentType("application/octet-stream;charset=utf-8");  
	        response.setHeader("Content-Disposition", "attachment;filename="  
				     + new String(admissionData.getBytes(),"iso-8859-1") + ".xlsx");
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);   
	        XSSFWorkbook workbook=null;  
	        //导出Excel对象  
	        workbook = admissionService.exportExcelInfo(admissionData);  
	        OutputStream output;  
	        try {
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
	            bufferedOutPut.flush();  
	            workbook.write(bufferedOutPut);  
	            bufferedOutPut.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
}